﻿/*
*      
*          ┌─┐       ┌─┐
*       ┌──┘ ┴───────┘ ┴──┐
*       │                 │
*       │       ───       │
*       │  ─┬┘       └┬─  │
*       │                 │
*       │       ─┴─       │
*       │                 │
*       └───┐         ┌───┘
*           │         │
*           │         │
*           │         │
*           │         └──────────────┐
*           │                        │
*           │                        ├─┐
*           │                        ┌─┘    
*           │                        │
*           └─┐  ┐  ┌───────┬──┐  ┌──┘         
*             │ ─┤ ─┤       │ ─┤ ─┤         
*             └──┴──┘       └──┴──┘ 
*                 神兽保佑 
*                 代码无BUG! 
*  Author shang
*  2022-07-21 18:09:05
*/

using Serilog;
using SqlSugar;

namespace TS.Common.Db;

public class DbContext
{
    static HttpRuntimeCache cache;
    private static object lockobj = new object();

      public static SqlSugarScope Db = new(new ConnectionConfig()
        {
            DbType =DbType.Sqlite,
            ConnectionString = "DataSource=" +  Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Task.sqlite"),
            IsAutoCloseConnection = true,
            ConfigureExternalServices = new ConfigureExternalServices()
            {
                DataInfoCacheService = GetMyCahce(), //配置我们创建的缓存类
            },
            InitKeyType = SqlSugar.InitKeyType.Attribute,
            MoreSettings = new ConnMoreSettings() 
            {
                IsWithNoLockQuery = true,
            }
        },
        db =>
        {
            db.Ado.IsEnableLogEvent = true; //启用日志事件
            db.Aop.OnLogExecuted = (sql, par) =>
            {
                string log = sql;
                log += "\r\n";
                log += string.Format("执行时间：{0:F2}毫秒。", db.Ado.SqlExecutionTime.TotalMilliseconds);
                log += "\r\n";
                log += "参数:";
                foreach (var item in par)
                {
                    log += item.ParameterName + ":  " + item.Value + "\r\n";
                }

                ///记录超过1s得sql
                if (db.Ado.SqlExecutionTime.TotalMilliseconds > 1000)
                    Log.Information(log);
#if DEBUG
                Console.WriteLine(log);
#endif
            };
        });


    public static ISqlSugarClient Init()
    {
        var client = new SqlSugarClient(new ConnectionConfig()
        {
            DbType =DbType.Sqlite,
            ConnectionString = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Task.sqlite"),
            IsAutoCloseConnection = true,
            ConfigureExternalServices = new ConfigureExternalServices()
            {
                DataInfoCacheService = GetMyCahce(), //配置我们创建的缓存类
            },
            InitKeyType = SqlSugar.InitKeyType.Attribute,
            MoreSettings = new ConnMoreSettings()
            {
                IsWithNoLockQuery = true,
                DisableNvarchar = true
            }
        });


        client.Ado.IsEnableLogEvent = true;
        client.Aop.OnLogExecuted = (sql, par) =>
        {
            string log = sql;
            log += "\r\n";
            log += string.Format("执行时间：{0:F2}毫秒。", client.Ado.SqlExecutionTime.TotalMilliseconds);
            log += "\r\n";
            log += "参数:";
            foreach (var item in par)
            {
                log += item.ParameterName + ":  " + item.Value + "\r\n";
            }

            ///记录超过1s得sql
            if (client.Ado.SqlExecutionTime.TotalMilliseconds > 1000)
                Log.Information(log);
#if DEBUG
            Console.WriteLine(log);
#endif
        };


        return client;
    }

    
    
    public static ICacheService GetMyCahce()
    {
        lock (lockobj)
        {
            if (cache == null)
                cache = new HttpRuntimeCache();
        }
        return cache;
    }
}